# -*- coding: utf-8 -*-
import pika
import sys

hostname = '117.34.118.30'
port = 25672
username = 'admin'
password = '1MKXQUIN'

credentials = pika.PlainCredentials(username=username, password=password)
print("credentials: %s" % credentials)
parameters = pika.ConnectionParameters(host=hostname,port=port,credentials=credentials)
print("parameters: %s" % parameters)
connection = pika.BlockingConnection(parameters=parameters)#创建连接
print("connection: %s" % connection)

channel = connection.channel()
print("channel: %s" % channel)
channel.exchange_declare(exchange='amq.topic', exchange_type='topic', durable='True')

result = channel.queue_declare(exclusive=False, queue='')
print("result: %s" % result)
queue_name = result.method.queue
print("queue_name: %s" % queue_name)

#绑定键。‘#’匹配所有字符，‘*’匹配一个单词
binding_keys = ['pos.base.*.*']

if not binding_keys:
    sys.stderr.write("Usage: %s [binding_key]...\n" % sys.argv[0])
    sys.exit(1)

for binding_key in binding_keys:
    channel.queue_bind(exchange='amq.topic', queue=queue_name, routing_key=binding_key)

print('[*] Writing for logs. To exit press CTRL+C.')

def callback(ch, method, properties, body):
    print(" [x] %r:%r" % (method.routing_key, body))

channel.basic_consume(queue=queue_name, on_message_callback=callback)

channel.start_consuming()
